package org.jeecg.modules.demo.ddyht.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.ddyht.entity.DdygCzrz;
import org.jeecg.modules.demo.ddyht.service.IDdygCzrzService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 操作日志
 * @Author: jeecg-boot
 * @Date:   2025-02-23
 * @Version: V1.0
 */
@Api(tags="操作日志")
@RestController
@RequestMapping("/ddyht/jxcCzrz")
@Slf4j
public class DdygCzrzController extends JeecgController<DdygCzrz, IDdygCzrzService> {
	@Autowired
	private IDdygCzrzService jxcCzrzService;

	 @PostMapping("/queryalltwo")
	 @ResponseBody
	 public Result queryalltwo(@RequestBody JSONObject params) {
		 String companyId = params.getString("companyId");
		 String paymethod = params.getString("paymethod");
		 return Result.ok(jxcCzrzService.getByalltwo(companyId, paymethod));
	 }
	 @PostMapping("/queryallthree")
	 @ResponseBody
	 public Result queryallthree(@RequestBody JSONObject params) {
		 String companyId = params.getString("companyId");
		 String type = params.getString("type");
		 return Result.ok(jxcCzrzService.getByalltwo(companyId, type));
	 }

	 @PostMapping("/returnlist")
	 @ResponseBody
	 public Result returnlist(@RequestBody JSONObject params) {
		 String username = params.getString("username");
		 Integer pageNo = params.getInteger("pageNo");
		 Integer pageSize = params.getInteger("pageSize");
		 
		 if (pageNo == null) pageNo = 1;
		 if (pageSize == null) pageSize = 10;
		 
		 List<DdygCzrz> allData = jxcCzrzService.getReturnBycompany(username);
		 int total = allData.size();
		 
		 // 计算分页
		 int startIndex = (pageNo - 1) * pageSize;
		 int endIndex = Math.min(startIndex + pageSize, total);
		 
		 List<DdygCzrz> pageData = allData.subList(startIndex, endIndex);
		 
		 // 构建分页结果
		 Page<DdygCzrz> page = new Page<>(pageNo, pageSize);
		 page.setTotal(total);
		 page.setRecords(pageData);
		 page.setPages((total + pageSize - 1) / pageSize);
		 
		 return Result.ok(page);
	 }

	/**
	 * 分页列表查询
	 *
	 * @param ddygCzrz
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "操作日志-分页列表查询")
	@ApiOperation(value="操作日志-分页列表查询", notes="操作日志-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<DdygCzrz>> queryPageList(DdygCzrz ddygCzrz,
												 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
												 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
												 HttpServletRequest req) {
		QueryWrapper<DdygCzrz> queryWrapper = QueryGenerator.initQueryWrapper(ddygCzrz, req.getParameterMap());
		Page<DdygCzrz> page = new Page<DdygCzrz>(pageNo, pageSize);
		IPage<DdygCzrz> pageList = jxcCzrzService.page(page, queryWrapper);
		List<DdygCzrz> company = jxcCzrzService.getBycompany(ddygCzrz.getUsername());
		int size = company.size();
		int page2 = (size + 9) / 10;
		pageList.setTotal(size);
		pageList.setPages(page2);
		pageList.setRecords(company);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param ddygCzrz
	 * @return
	 */
	@AutoLog(value = "操作日志-添加")
	@ApiOperation(value="操作日志-添加", notes="操作日志-添加")
	@RequiresPermissions("ddyht:jxc_czrz:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody DdygCzrz ddygCzrz) {
		jxcCzrzService.save(ddygCzrz);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param ddygCzrz
	 * @return
	 */
	@AutoLog(value = "操作日志-编辑")
	@ApiOperation(value="操作日志-编辑", notes="操作日志-编辑")
	@RequiresPermissions("ddyht:jxc_czrz:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody DdygCzrz ddygCzrz) {
		jxcCzrzService.updateById(ddygCzrz);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "操作日志-通过id删除")
	@ApiOperation(value="操作日志-通过id删除", notes="操作日志-通过id删除")
	@RequiresPermissions("ddyht:jxc_czrz:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		jxcCzrzService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "操作日志-批量删除")
	@ApiOperation(value="操作日志-批量删除", notes="操作日志-批量删除")
	@RequiresPermissions("ddyht:jxc_czrz:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.jxcCzrzService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "操作日志-通过id查询")
	@ApiOperation(value="操作日志-通过id查询", notes="操作日志-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<DdygCzrz> queryById(@RequestParam(name="id",required=true) String id) {
		DdygCzrz ddygCzrz = jxcCzrzService.getById(id);
		if(ddygCzrz ==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(ddygCzrz);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param ddygCzrz
    */
    @RequiresPermissions("ddyht:jxc_czrz:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, DdygCzrz ddygCzrz) {
        return super.exportXls(request, ddygCzrz, DdygCzrz.class, "操作日志");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("ddyht:jxc_czrz:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, DdygCzrz.class);
    }

}
